﻿using System;
using System.Reactive.Linq;
using Caliburn.Micro;
using Wulong.Phone.Dianping.Models;
using Wulong.Phone.Dianping.Services;

namespace Wulong.Phone.Dianping.ViewModels
{
    public class PromotionItemViewModel : DianpingViewModelBase
    {
        private IDisposable _disposable;
        private bool _isBusy;
        public bool IsBusy
        {
            get { return _isBusy; } 
            set
            {
                _isBusy = value;
                NotifyOfPropertyChange(()=>IsBusy);
            }
        }

        private readonly INavigationService _navigation;
        public PromotionItemViewModel(
            string cityCode, 
            string promoId,
            INavigationService navigation
            )
        {
            _navigation = navigation;
            PromoId = promoId;
            CityCode = cityCode;
            Load();
        }

        private void Load()
        {
            IsBusy = true;
            ButtonAvailable = false;
            _disposable=DianpingService.ObservablePromotionItem(CityCode, PromoId)
                .ObserveOnDispatcher()
                .Finally(() => IsBusy=false)
                .Subscribe(item =>
                {
                    //PromotionItem.MerchantString = item.MerchantString;
                    PromotionItem.Notice = item.Notice;
                    PromotionItem.Content = item.Content;
                    NotifyOfPropertyChange(() => PromotionItem);
                },
                ()=>ButtonAvailable = true);
        }
        public string PromoId { get; set; }

        private Promotion _promotionItem;
        public Promotion PromotionItem
        {
            get { return _promotionItem; }
            set
            {
                _promotionItem = value;
                NotifyOfPropertyChange(()=>PromotionItem);
            }
        }

        public string SendLink { get; set; }
        public bool IsSendLink { get; set; }
        private bool _buttonsAvailable;
        public bool ButtonAvailable
        {
            get { return _buttonsAvailable; }
            set
            {
                _buttonsAvailable = value;
                NotifyOfPropertyChange(()=>ButtonAvailable);
            }
        }
        public void SendToMyPhone()
        {
            IsSendLink = true;
            NotifyOfPropertyChange(()=>IsSendLink);
            SendLink = DianpingService.SendToMyPhoneLink(CityCode, PromoId);
            NotifyOfPropertyChange(()=>SendLink);
        }

        public void ViewMerchants()
        {
            _navigation.UriFor<SearchViewModel>()
                .WithParam(x=>x.CityCode, CityCode)
                .WithParam(x=>x.Keywords, PromotionItem.Title)
                .Navigate();
            TryClose();
        }

        protected override void OnDeactivate(bool close)
        {
            base.OnDeactivate(close);
            if(_disposable!=null)
                _disposable.Dispose();
        }
    }
}
